// utils/common.jsx
export const getDomain = (href = document.baseURI || window.location.href) => {
  const url = new URL(href);
  return url.hostname;
};

export const generateCopy = async (text) => {
  try {
    // Primary method using Clipboard API
    if (navigator.clipboard) {
      await navigator.clipboard.writeText(text);
      return true;
    }
    
    // Fallback for older browsers
    const textArea = document.createElement('textarea');
    textArea.value = text;
    
    // Avoid scrolling to bottom
    textArea.style.top = '0';
    textArea.style.left = '0';
    textArea.style.position = 'fixed';
    textArea.style.opacity = '0';
    
    document.body.appendChild(textArea);
    textArea.focus({ preventScroll: true });
    textArea.select();
    
    try {
      const successful = document.execCommand('copy');
      document.body.removeChild(textArea);
      return successful;
    } catch (err) {
      console.error('Fallback: Oops, unable to copy', err);
      document.body.removeChild(textArea);
      return false;
    }
  } catch (err) {
    console.error('Copy failed', err);
    return false;
  }
};

/**
 * Utility function to determine if the current path is the home page
 * @param {string} pathname - The current location pathname
 * @returns {boolean} - True if the path is the home page
 */
export const getHomeType = (pathname) => {
  return pathname === '/' || pathname === '/home-page';
};

/**
 * Utility function to determine which layout is being used based on the current path
 * @param {string} pathname - The current location pathname
 * @returns {Object} - Object containing boolean flags for different page types
 */
export const getPageTypes = (pathname) => {
  // Routes that don't use PublicLayout
  const isAuthPage = pathname.startsWith('/auth/');
  const isDashboardPage = pathname.startsWith('/dashboard');
  const isTeamsPage = pathname.startsWith('/teams/') || pathname === '/teams';
  const isUserTeamsPage = pathname.startsWith('/user-teams');
  const isCardAddPage = pathname.startsWith('/card/add');
  const isSubscribersPage = pathname.startsWith('/subscribers');
  const isPlanPage = pathname.startsWith('/plan');
  const isUserPage = pathname.startsWith('/user/');
  const isAccountPage = pathname.startsWith('/account/');
  const isUsersPage = pathname === '/users' || pathname.startsWith('/edit-user/') || pathname.startsWith('/create-user');
  const isWorkspacesPage = pathname.startsWith('/workspaces');

  // Check if current path is home page
  const isHomePage = getHomeType(pathname) || !(
    isAuthPage || 
    isDashboardPage || 
    isTeamsPage || 
    isUserTeamsPage || 
    isCardAddPage || 
    isSubscribersPage || 
    isPlanPage || 
    isUserPage || 
    isAccountPage || 
    isUsersPage || 
    isWorkspacesPage
  );

  return {
    isAuthPage,
    isDashboardPage,
    isTeamsPage,
    isUserTeamsPage,
    isCardAddPage,
    isSubscribersPage,
    isPlanPage,
    isUserPage,
    isAccountPage,
    isUsersPage,
    isWorkspacesPage,
    isHomePage
  };
};

/**
 * Custom hook that returns page type flags based on current route
 * @returns {Object} - Object containing boolean flags for different page types
 */
export const usePageTypes = () => {
  const { useLocation } = require('react-router-dom');
  const location = useLocation();
  return getPageTypes(location.pathname);
};

/**
 * Custom hook that returns whether the current page is a home page
 * @returns {boolean} - True if the current page is a home page
 */
export const useHomeType = () => {
  const { useLocation } = require('react-router-dom');
  const location = useLocation();
  return getHomeType(location.pathname);
};